// pages/template/register/register.js
var GlobalData = require('../../../utils/GlobalData.js');
var dataManager = require('../../../utils/dataManager.js');
var Util = require('../../../utils/util.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sysno: "",
    registerSys: {},
    mobile: 0,
    /**短信验证码 */
    codeMsg: "",
    name: "",
    isShow: false,
    isclick: true,
    pic: "",
    time: 0,
    btinfo: "获取验证码",
    messageTurn: true,
    // 获取验证码按钮
    turn: true,
    // 是否修改成功
    isSucee: false,
    timer: {},
    /**图片验证码 */
    code: "",
    url: "https://shopapi.queyouquan.cn/API/Agent.aspx",
    hidden: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options)
    // var option = "1F6E709EDDEB4031B8ECCBBD28D036F4"
    that.setData({
      sysno: options.sysno
    })
    if (GlobalData.registerSys.sysno) {
      // console.log(GlobalData.registerSys, "有数据")
      this.setData({
        registerSys: GlobalData.registerSys
      })
    } else {
      // console.log(GlobalData.registerSys, "meishuju数据")
      wx.request({
        url: that.data.url,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: {
          method: 'getagentbysysno',
          sysno: options.sysno
        },
        complete: function (res) {
          let pkg = res.data;
          if (pkg && pkg.errcode == 0) {
            GlobalData.registerSys = pkg.data;
            that.setData({
              registerSys: pkg.data
            })
          } else {
            app.onTip(pkg.errmsg)
            return;
          }
          // console.log(res);
        }
      })
    }
  },
  /**请输入手机号码 */
  onInputMobile: function (e) {
    var mobile = e.detail.value;
    // console.log(mobile, "手机号码");
    if (mobile) {
      this.setData({
        mobile: mobile
      })
    } else {
      // app.show("请输入手机号码")
    }

  },
  /**获取验证码 */
  getCode: function () {
    // app.show("获取验证码。");
    // console.log("请输入正确的手机号。", this.data.mobile);
    var that = this
    if (!that.data.turn)
      return;

    if (!that.data.mobile || that.data.mobile < 10000000000 || that.data.mobile > 99999999999) {
      // console.log(that.data.mobile, "获取验证码");
      app.show("请输入正确的手机号。");
      return;
    }
    var imgurl = "https://shopapi.queyouquan.cn/Tool/Codeimg.aspx" + "?mobile=" + that.data.mobile + "&" + Math.random();
    that.setData({
      pic: imgurl,
      isShow: true
    })
  },



  /**请输入验证码 */
  onInputCode: function (e) {
    var code = e.detail.value;
    if (code) {
      this.setData({
        code: code
      })
    } else {
      // app.show("请输入验证码")
    }

  },
  /**请输入姓名 */
  onInputName: function (e) {
    var name = e.detail.value;
    if (name) {
      this.setData({
        name: name
      })
    } else {
      // app.show("请输入姓名")
    }
  },
  /**请输入短信验证码 */
  onInputSMSCode: function (e) {
    var codeMsg = e.detail.value;
    if (codeMsg) {
      this.setData({
        codeMsg: codeMsg
      })
    } else {
      // app.show("请输入验证码")
    }
  },
  /**发送验证码 */
  onSetWithoutCode: function () {
    var that = this;
    if (!this.data.code || this.data.code == "") {
      app.show("请输入验证码")
      return;
    }
    //判断验证码是否为空
    if (this.data.messageTurn) {
      this.setData({
        messageTurn: false,
        isShow: false
      })

      wx.request({
        url: that.data.url,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: Util.json2Form({ method: "sendcode_forgetpass", "mobile": this.data.mobile, "picturecode": this.data.code, isvalidate: "0" }),
        complete: function (res) {
          let pkg = res.data;
          console.log(res)
          if (res && pkg && pkg.errcode == 0) {
            that.setData({
              turn: false
            })
            app.show("验证码已发送到您的手机。");
            //1分钟限制
            that.setData({
              turn: false,
              btinfo: "60s",
              time: 60
            })
            that.data.timer = setInterval(() => {
              that.data.time = that.data.time - 1
              that.setData({
                btinfo: that.data.time + "s"
              })

              if (that.data.time == 0) {
                that.setData({
                  turn: true,
                  btinfo: "获取验证码",
                })
                clearInterval(that.data.timer);
              }
            }, 1000);
            // that.data.checkcode = "";
            that.setData({
              messageTurn: true,
              msg: ""
            })
          } else {
            // console.log("账号不存在吗？");
            that.setData({
              messageTurn: true,
            })
            clearInterval(that.data.timer);
            // that.data.messageTurn = true;
            app.show(pkg.errmsg)

          }
        }
      })
    }
  },
  /**确认*/
  onConfirm: function () {
    // console.log("确认修改密码")
    var that = this

    if (!that.data.mobile) {
      app.show("请输入手机号码。");
      return;
    }
    else if (!that.data.codeMsg) {
      app.show("请输入短信验证码。");
      return;
    }
    else if (!that.data.name) {
      app.show("请输入新密码。");
      return;
    }
    that.setData({
      isSucee: true
    })
    this.setData({
      hidden: false
    })
    wx.request({
      url: that.data.url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "regist", "mobile": that.data.mobile, "code": that.data.codeMsg, "truename": that.data.name, "refereessysno": that.data.sysno }),
      complete: function (res) {
        let pkg = res.data;
        console.log(res)
        that.setData({
          isSucee: false
        })
        if (res && pkg && pkg.errcode == 0) {
          app.onTip("注册成功,登录密码以短信形式发送给您.", "/pages/login/login");
        } else {
          that.setData({
            // messageTurn: true,
            turn: true,
            btinfo: "获取验证码",
          })
          clearInterval(that.data.timer);
          app.show(pkg.errmsg)
        }
        that.setData({
          hidden: true
        })
      }
    })
  },
  onClose: function () {
    this.setData({
      isShow: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      hidden: true
    })



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})